<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>我的购物车</title>
    <link rel="icon" href="../img/favicon.ico">

    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" type="text/css" href="../css/all.css"/>
    <link rel="stylesheet" type="text/css" href="../css/pages-cart.css"/>

    <!--elementUI东西-->
    <script src="//unpkg.com/vue/dist/vue.js"></script>
    <script src="//unpkg.com/element-ui@2.15.7/lib/index.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>

<body>
<!--head-->
<!-- 头部栏位 -->
<!--页面顶部-->
<div id="nav-bottom">
    <!--顶部-->
    <div class="nav-top">
        <div class="top">
            <div class="py-container">
                <div class="shortcut">
                    <ul class="fl">
                        <li class="f-item">汇才商城欢迎您！</li>
                        <li class="f-item">
                            <a th:if="${session.user!=null}" href="#" th:text="${session.user.userName}"></a>
                            <a th:if="${session.user==null}" href="login.html">登录</a>
                            <span>
								<a th:if="${session.user==null}" href="register.html">免费注册</a>
								<a th:if="${session.user!=null}" href="javascript:;" onclick="userLogout()">退出</a>
							</span>
                        </li>
                    </ul>
                    <div class="fr typelist">
                        <ul class="types">
                            <li class="f-item">
									<span>
										<a href="myOrder.html" target="_blank">我的订单</a>
									</span>
                            </li>
                            <li class="f-item">
									<span>
										<a href="cart.html" target="_blank">我的购物车</a>
									</span>
                            </li>
                            <li class="f-item">
									<span>
										<a href="home.html" target="_blank">我的汇才</a>
									</span>
                            </li>
                            <li class="f-item">
                                <span>汇才会员</span>
                            </li>
                            <li class="f-item">
                                <span>企业采购</span>
                            </li>
                            <li class="f-item">
                                <span>关注汇才</span>
                            </li>
                            <li class="f-item">
									<span>
										<a href="cooperation.html" target="_blank">合作招商</a>
									</span>
                            </li>
                            <li class="f-item">
									<span>
										<a href="shoplogin.html" target="_blank">商家后台</a>
									</span>
                            </li>

                        </ul>
                    </div>

                </div>
            </div>
        </div>

        <!--头部-->
        <div class="header">
            <div class="py-container">
                <div class="yui3-g Logo">
                    <div class="yui3-u Left logoArea">
                        <a class="logo-bd" title="汇才商城" href="index.html"></a>
                    </div>
                    <div class="yui3-u Rit searchArea">
                        <div class="search">
                            <form action="" class="sui-form form-inline">
                                <!--searchAutoComplete-->
                                <div class="input-append">
                                    <input id="autocomplete" type="text" class="input-error input-xxlarge"/>
                                    <button class="sui-btn btn-xlarge btn-danger" type="button">搜索</button>
                                </div>
                            </form>
                        </div>
                    </div>

                </div>

            </div>
        </div>
    </div>
</div>


<div class="cart py-container" id="app">
    <!--All goods-->
    <div class="allgoods">
        <h4>全部商品
            <span></span>
        </h4>
        <div class="cart-main">
            <div class="yui3-g cart-th">
                <div class="yui3-u-1-4">
                    <input class="chooseAll" type="checkbox" name="selectAll1" @change="selectAll(1)"/> 全部
                </div>
                <div class="yui3-u-1-4">商品</div>
                <div class="yui3-u-1-8">单价（元）</div>
                <div class="yui3-u-1-8">数量</div>
                <div class="yui3-u-1-8">小计（元）</div>
                <div class="yui3-u-1-8">操作</div>
            </div>
            <div class="cart-item-list">

                <div class="cart-body">
                    <!-- vue loadlist  -->
                    <div class="cart-list" v-for="item in shopCartList" :key="item.index">
                        <ul class="goods-list yui3-g">
                            <li class="yui3-u-1-24">
                                <!--                                <input type="hidden" v-model="item.productId"></input>-->
                                <input type="checkbox" name="chk_list" v-model="item.isChecked"/>
                            </li>
                            <li class="yui3-u-6-24" style="height: 100px;">
                                <div class="good-item">
                                    <div class="item-img">
                                        <img :src="item.product.productPicture"/>
                                    </div>
                                    <div style="width: 500px">{{item.product.productIntroduction}}</div>
                                </div>
                            </li>
                            <li class="yui3-u-5-24">
                                <div class="item-txt">{{item.product.productName}}</div>
                            </li>
                            <li class="yui3-u-1-8">
                                <span class="price">{{(Math.round(item.product.productPrice*100)/100).toFixed(2)}}</span>
                            </li>
                            <li class="yui3-u-1-8">
                                <a href="javascript:void(0)" class="increment mins" @click="item.productNum>1?item.productNum--:1">-</a>
                                <input autocomplete="off" type="text" v-model="item.productNum"
                                       @keydown="item.productNum>1?item.productNum:(item.productNum=1)"
                                       style="width:50px" class="itxt"/>
                                <a href="javascript:void(0)" class="increment plus" @click="item.productNum++">+</a>
                            </li>
                            <li class="yui3-u-1-8">
                                <span class="sum">{{(Math.round(item.product.productPrice*item.productNum*100)/100).toFixed(2)}}</span>
                            </li>
                            <li class="yui3-u-1-8">
                                <a href="javascript:;" @click="deleteProductFromShopCart(item.shopCartId)">删除</a>
                                <br/>
                                <a href="#none">移到收藏</a>
                            </li>
                        </ul>
                    </div>

                </div>
            </div>
        </div>
        <div class="cart-tool">
            <div class="select-all">
                <input class="chooseAll" name="selectAll2" type="checkbox" @click="selectAll(2)"/>
                <span>全选</span>
            </div>
            <div class="option">
                <a href="javascript:;" @click="deleteProductFromShopCart()">删除选中的商品</a>
                <a href="javascript:;">移到我的关注</a>
                <a href="javascript:;">清除下柜商品</a>
            </div>
            <div class="money-box">
                <div class="chosed">已选择
                    <span>0</span>件商品
                </div>
                <div class="sumprice">
                    <span>
                        <em>总价（不含运费） ：</em>
                        <i class="summoney">¥{{(Math.round(totalMoney*100)/100).toFixed(2)}}</i>
                    </span>
                    <br>
                    <span>
							<em>已节省：</em>
							<i>-¥20.00</i>
						</span>
                </div>
                <div class="sumbtn">
                    <a class="sum-btn" href="javascript:;" @click="addOrder()">结算</a>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="liked">
            <ul class="sui-nav nav-tabs">
                <li class="active">
                    <a href="#index" data-toggle="tab">猜你喜欢</a>
                </li>
                <li>
                    <a href="#profile" data-toggle="tab">特惠换购</a>
                </li>
            </ul>
            <div class="clearfix"></div>
            <div class="tab-content">
                <div id="index" class="tab-pane active">
                    <div id="myCarousel" data-ride="carousel" data-interval="4000" class="sui-carousel slide">
                        <div class="carousel-inner">
                            <div class="active item">
                                <ul>
                                    <li>
                                        <img src="../img/like1.png"/>
                                        <div class="intro">
                                            <i>Apple苹果iPhone 6s (A1699)</i>
                                        </div>
                                        <div class="money">
                                            <span>$29.00</span>
                                        </div>
                                        <div class="incar">
                                            <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
                                                <i class="car"></i>
                                                <span class="cartxt">加入购物车</span>
                                            </a>
                                        </div>
                                    </li>
                                    <li>
                                        <img src="../img/like2.png"/>
                                        <div class="intro">
                                            <i>Apple苹果iPhone 6s (A1699)</i>
                                        </div>
                                        <div class="money">
                                            <span>$29.00</span>
                                        </div>
                                        <div class="incar">
                                            <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
                                                <i class="car"></i>
                                                <span class="cartxt">加入购物车</span>
                                            </a>
                                        </div>
                                    </li>
                                    <li>
                                        <img src="../img/like3.png"/>
                                        <div class="intro">
                                            <i>Apple苹果iPhone 6s (A1699)</i>
                                        </div>
                                        <div class="money">
                                            <span>$29.00</span>
                                        </div>
                                        <div class="incar">
                                            <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
                                                <i class="car"></i>
                                                <span class="cartxt">加入购物车</span>
                                            </a>
                                        </div>
                                    </li>
                                    <li>
                                        <img src="../img/like4.png"/>
                                        <div class="intro">
                                            <i>Apple苹果iPhone 6s (A1699)</i>
                                        </div>
                                        <div class="money">
                                            <span>$29.00</span>
                                        </div>
                                        <div class="incar">
                                            <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
                                                <i class="car"></i>
                                                <span class="cartxt">加入购物车</span>
                                            </a>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                            <div class="item">
                                <ul>
                                    <li>
                                        <img src="../img/like1.png"/>
                                        <div class="intro">
                                            <i>Apple苹果iPhone 6s (A1699)</i>
                                        </div>
                                        <div class="money">
                                            <span>$29.00</span>
                                        </div>
                                        <div class="incar">
                                            <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
                                                <i class="car"></i>
                                                <span class="cartxt">加入购物车</span>
                                            </a>
                                        </div>
                                    </li>
                                    <li>
                                        <img src="../img/like2.png"/>
                                        <div class="intro">
                                            <i>Apple苹果iPhone 6s (A1699)</i>
                                        </div>
                                        <div class="money">
                                            <span>$29.00</span>
                                        </div>
                                        <div class="incar">
                                            <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
                                                <i class="car"></i>
                                                <span class="cartxt">加入购物车</span>
                                            </a>
                                        </div>
                                    </li>
                                    <li>
                                        <img src="../img/like3.png"/>
                                        <div class="intro">
                                            <i>Apple苹果iPhone 6s (A1699)</i>
                                        </div>
                                        <div class="money">
                                            <span>$29.00</span>
                                        </div>
                                        <div class="incar">
                                            <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
                                                <i class="car"></i>
                                                <span class="cartxt">加入购物车</span>
                                            </a>
                                        </div>
                                    </li>
                                    <li>
                                        <img src="../img/like4.png"/>
                                        <div class="intro">
                                            <i>Apple苹果iPhone 6s (A1699)</i>
                                        </div>
                                        <div class="money">
                                            <span>$29.00</span>
                                        </div>
                                        <div class="incar">
                                            <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
                                                <i class="car"></i>
                                                <span class="cartxt">加入购物车</span>
                                            </a>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </div>
                        <a href="#myCarousel" data-slide="prev" class="carousel-control left">‹</a>
                        <a href="#myCarousel" data-slide="next" class="carousel-control right">›</a>
                    </div>
                </div>
                <div id="profile" class="tab-pane">
                    <p>特惠选购</p>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 底部栏位 -->
<!--页面底部-->
<div class="clearfix footer">
    <div class="py-container">
        <div class="footlink">
            <div class="clearfix Mod-list">
                <div class="yui3-g">
                    <div class="yui3-u-1-6">
                        <h4>购物指南</h4>
                        <ul class="unstyled">
                            <li>购物流程</li>
                            <li>会员介绍</li>
                            <li>生活旅行/团购</li>
                            <li>常见问题</li>
                            <li>购物指南</li>
                        </ul>

                    </div>
                    <div class="yui3-u-1-6">
                        <h4>配送方式</h4>
                        <ul class="unstyled">
                            <li>上门自提</li>
                            <li>211限时达</li>
                            <li>配送服务查询</li>
                            <li>配送费收取标准</li>
                            <li>海外配送</li>
                        </ul>
                    </div>
                    <div class="yui3-u-1-6">
                        <h4>支付方式</h4>
                        <ul class="unstyled">
                            <li>货到付款</li>
                            <li>在线支付</li>
                            <li>分期付款</li>
                            <li>邮局汇款</li>
                            <li>公司转账</li>
                        </ul>
                    </div>
                    <div class="yui3-u-1-6">
                        <h4>售后服务</h4>
                        <ul class="unstyled">
                            <li>售后政策</li>
                            <li>价格保护</li>
                            <li>退款说明</li>
                            <li>返修/退换货</li>
                            <li>取消订单</li>
                        </ul>
                    </div>
                    <div class="yui3-u-1-6">
                        <h4>特色服务</h4>
                        <ul class="unstyled">
                            <li>夺宝岛</li>
                            <li>DIY装机</li>
                            <li>延保服务</li>
                            <li>汇才E卡</li>
                            <li>汇才通信</li>
                        </ul>
                    </div>
                    <div class="yui3-u-1-6">
                        <h4>帮助中心</h4>
                        <img src="../img/wx_cz.jpg">
                    </div>
                </div>
            </div>
            <div class="Mod-copyright">
                <ul class="helpLink">
                    <li>关于我们
                        <span class="space"></span>
                    </li>
                    <li>联系我们
                        <span class="space"></span>
                    </li>
                    <li>关于我们
                        <span class="space"></span>
                    </li>
                    <li>商家入驻
                        <span class="space"></span>
                    </li>
                    <li>营销中心
                        <span class="space"></span>
                    </li>
                    <li>友情链接
                        <span class="space"></span>
                    </li>
                    <li>关于我们
                        <span class="space"></span>
                    </li>
                    <li>营销中心
                        <span class="space"></span>
                    </li>
                    <li>友情链接
                        <span class="space"></span>
                    </li>
                    <li>关于我们</li>
                </ul>
                <p>© 2020 版权所有 广州汇才创智科技有限公司</p>
                <p>粤ICP备20043971号</p>
            </div>
        </div>
    </div>
</div>
<!--页面底部END-->
<script type="text/javascript" src="../js/jquery-3.4.1.min.js"></script>
<!-- 引入组件库 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.8/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/element-ui/2.5.4/index.js"></script>

<script src="../js/vue.js"></script>
<script src="../js/axios.js"></script>

<script type="text/javascript">
    /*$(".chooseAll").click(function () {
        $("input[name='chk_list']").prop("checked", $(this).prop("checked"));
        /!*同步 全部选择按钮和全选按钮*!/
        $(".chooseAll").prop("checked", $(this).prop("checked"));
        // console.log($("input[name='chk_list']").val())
    })*/

    new Vue({
        el: '#app',
        data: {
            shopCartList: [],
            selectShopCartList: [],
            totalMoney: 0.00,
            shopCatId:'',  //缓存选中的购物车id
        },
        created() {
            this.getShopCart();
        },
        methods: {
            isUserLogin(){
                let url = "/SpringBoot/user/isUserLogin";
                let _this = this;
                axios.post(url).then(function (response) {
                    if (response.data.success) {
                        this.getShopCart();
                    } else {
                        window.open("login");
                        alert(response.data.message)
                    }
                }).catch(function (error) {
                    console.log(error);
                })
            },
            /*全选*/
            selectAll(index) {
                let isSelectAll = $("input[name=" + 'selectAll' + index + "]").prop("checked");
                console.log(isSelectAll)
                $("input[name='chk_list']").prop("checked", isSelectAll);
                /*同步 全部选择按钮和全选按钮*/
                $(".chooseAll").prop("checked", isSelectAll);
                this.shopCartList.forEach(function (item) {
                    item.isChecked = isSelectAll;
                });
            },
            /*获取用户的购物车的信息*/
            getShopCart() {
                let url = "/SpringBoot/user/getShopCartByUid";
                let _this = this;
                axios.post(url).then(function (response) {
                    if (response.data.success) {
                        _this.shopCartList = response.data.shopCartList;
                    } else {
                        alert(response.data.message)
                    }
                }).catch(function (error) {
                    console.log(error);
                })
            },
            /*删除购物车中的商品*/
            deleteProductFromShopCart(pid) {
                let url = "/SpringBoot/user/deleteProductFromShopCart";
                let _this = this;
                let shopCatIds='';
                if (pid == undefined) {
                    if (this.selectShopCartList.length <= 0) {
                        alert("请选择您需要删除的商品")
                        return;
                    }else {
                        shopCatIds=_this.shopCatId;
                    }
                } else {
                    shopCatIds = pid + ",";
                }
                console.log(shopCatIds);
                axios.get(url, {params:{shopCatIds}}).then(function (response) {
                    _this.getShopCart();
                    alert(response.data.message)
                }).catch(function (error) {
                    console.log(error);
                })
            },
            /*添加订单*/
            addOrder() {
                let url = "/SpringBoot/user/addOrder";
                let _this = this;
                console.log(_this.selectShopCartList);
                axios.post(url,_this.selectShopCartList).then(function (response) {
                    if (response.data.success) {
                        location.reload();
                    }
                    alert(response.data.message)
                }).catch(function (error) {
                    console.log(error);
                })
            }
        },
        watch: {
            shopCartList: {
                deep: true,
                handler: function () {
                    let itemMoney = parseFloat(0);
                    let itemSelect = [];
                    let itemShopCatId='';
                    // console.log(this.totalMoney)
                    this.shopCartList.forEach(function (item) {
                        if (item.isChecked) {
                            itemMoney += (item.productNum) * (item.product.productPrice);
                            // console.log(itemMoney);
                            itemSelect.push(item);
                            itemShopCatId += item.shopCartId + ",";
                        }
                    })
                    this.totalMoney = itemMoney;
                    this.selectShopCartList = itemSelect;
                    this.shopCatId=itemShopCatId;
                    // console.log(itemSelect)
                }
            }
        }
    })

    /*退出登录*/
    function userLogout() {
        let flag = confirm("您确定要退出登录吗?")
        if (flag) {
            $.ajax({
                url: "/SpringBoot/user/userLogout",
                type: "post",
                dataType: "json",
                data: "",
                success: function (data) {
                    if (data.success) {
                        /*刷新当前页面*/
                        location.reload();
                    }
                },
                error: function () {
                    $("#message").html("数据获取失败");
                }
            })
        }
    }
</script>

</body>

</html>
